//
//  HSYCustomPhotoViewController.m
//  高仿暴走斗图
//
//  Created by key on 16/6/3.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYCustomPhotoViewController.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "CustomPhotosCell.h"
#import "NSObjcGCD.h"
#import "HSYBaseViewController+ALAsset.h"
#import "HSYCustomPhotoModel.h"

#define PHOTOS_IDENTIFIER     @"HSYPhotosIdentifier"

@interface HSYCustomPhotoViewController ()

@property (nonatomic, strong) HSYCustomPhotoModel *viewModel;

@end

@implementation HSYCustomPhotoViewController

- (instancetype)init {
    
    if (self = [super init]) {
    
        _viewModel = [[HSYCustomPhotoModel alloc] init];
        [self createPhotoLibrary];
    }
    return self;
}

- (instancetype)initWithALAssetType:(kALAssetType)type {
    
    if (self = [super init]) {
        
        _viewModel = [[HSYCustomPhotoModel alloc] init];
        [self createPhotoLibrary];
    }
    return self;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createCollectionViewInParentView:self.view
                                      rect:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-[self getTopBarHeight])
                              sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)
                        minimumLineSpacing:10
                   minimumInteritemSpacing:10
                                  itemSize:CGSizeMake(100, 100)
                       headerReferenceSize:CGSizeMake(0, 0)
                       footerReferenceSize:CGSizeMake(0, 0)];
    [self.collectionView registerClass:[CustomPhotosCell class] forCellWithReuseIdentifier:PHOTOS_IDENTIFIER];
    @weakify(self);
    self.selectIndexPathCell = ^(id tableViewCell, NSIndexPath *indexPath) {
     
        @strongify(self);
        CustomPhotosCell *cell = (CustomPhotosCell *)tableViewCell;
        BOOL isAddObject = [cell transformsAnimation];
        [self.viewModel reloadDataForIsAddObject:isAddObject withIndex:indexPath.row];
    };
    
    [self removeNavigationBarLeftButton];
    [self setNavigationBarLeftButtonWithImage:BACK_IMAGE_NOR hightImage:BACK_IMAGE_PRE action:@selector(dismiss:) open:YES];
    
    [self setNavigationBarRightButtonWithTitle:HSYLOCALIZED(@"完成")];
    self.clickRightButtonBlock = ^(UIButton *button) {
        @strongify(self);
        if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectCompletionImages:)]) {
            [self.delegate didSelectCompletionImages:self.viewModel.fullScreenImages];
            [self dismiss:nil];
        }
    };
}


- (void)dismiss:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createPhotoLibrary {
    
    @weakify(self);
    [[[self rac_openPhotoLibraryWithALAssetType:self.viewModel.assetType] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(ALAsset *asset) {
        
        @strongify(self);
        [self.viewModel photosAddObject:@{
                                          @(NO) : asset
                                          }];
    } error:^(NSError *error) {
        
        @strongify(self);
        [self.viewModel photosRemoveAllObjects];
    } completed:^{
        
        @strongify(self);
        [self.collectionView reloadData];
        
    }];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.viewModel.photos.count > 0) {
        return self.viewModel.photos.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {

    static NSString *kHSYPhotosIdentifier = PHOTOS_IDENTIFIER;
    CustomPhotosCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHSYPhotosIdentifier forIndexPath:indexPath];
    [cell sizeToFit];
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHSYPhotosIdentifier forIndexPath:indexPath];
    }
    
    NSDictionary *dic = self.viewModel.photos[indexPath.row];
    UIImage *image = [UIImage imageWithCGImage:[[dic.allValues firstObject] thumbnail]];
    [cell setPhotoImage:image];
    cell.isSelected = [[dic.allKeys firstObject] boolValue];

    return cell;
}

@end
